package com.dgshanger.sanhxiaofeisc.ui.person

import android.Manifest
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import com.blankj.utilcode.util.PermissionUtils
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.entity.ShareEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.SharedContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.SharedPresenter
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import com.github.yoojia.qrcode.qrcode.QRCodeEncoder
import com.umeng.socialize.ShareAction
import com.umeng.socialize.UMShareAPI
import com.umeng.socialize.UMShareListener
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage
import com.umeng.socialize.media.UMWeb
import com.xx.baseuilibrary.mvp.BaseMvpActivity
import kotlinx.android.synthetic.main.activity_shared.*


/**
 * SharedActivity
 * 沉迷学习不能自拔
 * Describe：分享页面
 * Created by 雷小星🍀 on 2018/4/16 11:18.
 */
class SharedActivity : BaseMvpActivity<SharedContract.Model, SharedContract.View, SharedPresenter>(),
        SharedContract.View {

    private var entity: ShareEntity? = null

    override fun setData(entity: ShareEntity?) {
        with(entity!!) {
            this@SharedActivity.entity = this
            ImageLoader.loadAvatar(mContext, iv_user_head, avatar)
            tv_user_nickname.text = nick_name
            tv_code.text = show_id
            tv_link.text = ImageLoader.getUrl("/" + share_qrcode_linik)
            iv_qr.setImageBitmap(QRCodeEncoder.Builder()
                    .width(360)
                    .height(360)
                    .paddingPx(0)
                    .marginPt(0)
                    .centerImage(mContext, R.mipmap.ic_launcher)
                    .build()
                    .encode(ImageLoader.getUrl("/" + share_qrcode_linik)))
        }
    }

    override fun createPresenter(): SharedPresenter = SharedPresenter()

    override fun getActivityLayoutId(): Int = R.layout.activity_shared

    override fun initData() {
        getPresenter().loadData()
    }

    override fun initEvent() {
        iv_weibo.setOnClickListener {
            //分享到微博
            share(SHARE_MEDIA.SINA)
        }
        iv_qq.setOnClickListener {
            //分享到qq
            share(SHARE_MEDIA.QQ)
        }

        iv_wechatq.setOnClickListener {
            //分享到朋友圈
            share(SHARE_MEDIA.WEIXIN_CIRCLE)
        }

        iv_wechat.setOnClickListener {
            //分享到微信
            share(SHARE_MEDIA.WEIXIN)
        }
        tv_link.setOnClickListener {
            //点击链接,复制到粘贴板
            val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            cm.primaryClip = ClipData.newPlainText("分享链接", tv_link.text)
            showToast("成功复制链接")
        }
    }

    //分享监听反馈
    private var shareListener = object : UMShareListener {
        override fun onResult(p0: SHARE_MEDIA?) {
            showToast(getString(R.string.sharing_success))
        }

        override fun onCancel(p0: SHARE_MEDIA?) {
            showToast(getString(R.string.Stop_sharing))
        }

        override fun onError(p0: SHARE_MEDIA?, p1: Throwable?) {
            showToast(p1?.message)
        }

        override fun onStart(p0: SHARE_MEDIA?) {
        }
    }

    /**
     * 分享
     */
    private fun share(share_media: SHARE_MEDIA) {
        PermissionUtils
                .permission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .callback(object : PermissionUtils.SimpleCallback {
                    override fun onGranted() {
                        ShareAction(this@SharedActivity)
                                .apply {
                                    platform = share_media
                                    withMedia(UMWeb(ImageLoader.getUrl("/" + entity!!.share_qrcode_linik))
                                            .apply {
                                                this.title = entity!!.share_title
                                                this.description = entity!!.share_introduce
                                                this.setThumb(UMImage(mContext, BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)))
                                            })
                                }
                                .setCallback(shareListener)
                                .share()
                    }

                    override fun onDenied() {
                        showToast("拒绝权限将导致该功能无法正常使用")
                    }

                })
                .rationale(object : PermissionUtils.OnRationaleListener {
                    override fun rationale(shouldRequest: PermissionUtils.OnRationaleListener.ShouldRequest?) {
                        shouldRequest!!.again(true)
                    }

                })
                .request()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data)
    }
}